package edu.uwm.cs552;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.MalformedURLException;
import java.net.URL;

public class HexBoardDialog extends JDialog {
	
	private static final long serialVersionUID = 1L;
	
	private int width, height;
	private URL url;

	public HexBoardDialog(JFrame frame) {	
		super(frame, "Create Hex board", true);
		width = 0;
		height = 0;
		url = null;
		
		// Components
		JLabel urlLabel = new JLabel("Background URL:");
		final JTextField textURL = new JTextField();
		textURL.setSize(40, 10);
		JLabel widthLabel = new JLabel("width:");
		final JTextField textWidth = new JTextField();
		JLabel heightLable = new JLabel("height:");
		final JTextField textHeight = new JTextField();
		JButton cancel = new JButton("Cancel");
		JButton ok = new JButton("OK");
		
		// Add URL part
		Box upperTop = Box.createHorizontalBox();
		upperTop.add(Box.createHorizontalGlue());
		upperTop.add(urlLabel);
		upperTop.add(Box.createHorizontalGlue());
		Box lowerTop = Box.createHorizontalBox();
		lowerTop.add(Box.createRigidArea(new Dimension(5, 0)));
		lowerTop.add(textURL);
		lowerTop.add(Box.createRigidArea(new Dimension(5, 0)));
		Box top = Box.createVerticalBox();
		top.add(upperTop);
		top.add(lowerTop);
		
		// Add selection part
		Box center = Box.createHorizontalBox();
		center.add(Box.createRigidArea(new Dimension(50, 0)));
		center.add(widthLabel);
		center.add(Box.createRigidArea(new Dimension(4, 0)));
		center.add(textWidth);
		center.add(Box.createRigidArea(new Dimension(15, 0)));
		center.add(heightLable);
		center.add(Box.createRigidArea(new Dimension(4, 0)));
		center.add(textHeight);
		center.add(Box.createRigidArea(new Dimension(50, 0)));
		
		// Add button part
		Box bottom = Box.createHorizontalBox();
		bottom.add(Box.createHorizontalGlue());
		bottom.add(cancel);
		bottom.add(Box.createRigidArea(new Dimension(5, 0)));
		bottom.add(ok);
		bottom.add(Box.createHorizontalGlue());
		
		// Add all parts together
		Box all = Box.createVerticalBox();
		all.add(Box.createRigidArea(new Dimension(0, 5)));
		all.add(top);
		all.add(Box.createRigidArea(new Dimension(0, 30)));
		all.add(center);
		all.add(Box.createRigidArea(new Dimension(0, 30)));
		all.add(bottom);
		all.add(Box.createRigidArea(new Dimension(0, 20)));
		
		// Add to frame
		Container content = this.getContentPane();
		content.setLayout(new BorderLayout());
		content.add(all, BorderLayout.CENTER);
		
		// Add Listeners
		cancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				doCancel();
			}
		});
		
		ok.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				doOk(textWidth, textHeight, textURL);
			}
		});
	}
	
	public int getTextWidth() {
		return width;
	}

	public int getTextHeight() {
		return height;
	}

	public URL getTextURL() {
		return url;
	}
	
	private void doCancel() {
		this.setVisible(false);
	}
	
	private void doOk(JTextField textWidth, JTextField textHeight, JTextField textURL) {
		try {
			if(textURL.getText().trim().equals("")) url = null;
			else url = new URL(textURL.getText());
			width = Integer.parseInt(textWidth.getText());
			height = Integer.parseInt(textHeight.getText());
			
			if(width <= 0)
				JOptionPane.showMessageDialog(this, "Width must be positive", "Range Error", JOptionPane.ERROR_MESSAGE);
			if(height <= 0)
				JOptionPane.showMessageDialog(this, "Height must be positive", "Range Error", JOptionPane.ERROR_MESSAGE);
			else
				this.setVisible(false);
				
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(this, "Width and Height must be an integer", "Invalid Entry Error", JOptionPane.ERROR_MESSAGE);
		} catch (MalformedURLException e) {
			JOptionPane.showMessageDialog(this, "URL is not valid", "Invalid Entry Error", JOptionPane.ERROR_MESSAGE);
		}
	}	
}
